package com.morningstar.flow.antvX6.node;

import com.alibaba.fastjson2.JSON;
import com.morningstar.flow.antvX6.common.Node;
import com.morningstar.flow.antvX6.common.NodeParser;
import com.morningstar.flow.flowable.assigne.AssignMultiStrategy;
import com.morningstar.flow.flowable.listener.AssigneeListener;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.FlowableListener;
import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.UserTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 用户审批节点
 *
 * @author Admin
 */
@Setter
@Getter
public class NodeUserTask extends Node implements NodeParser {

    public static final String TYPE = "node_user_task";

    @Override
    public String getNodeType() {
        return TYPE;
    }

    @Override
    public String getNodeName() {
        if (this.getData() != null && StringUtils.isNotBlank(this.getData().getLabel())) {
            return this.getData().getLabel();
        }
        return "审批节点";
    }

    @Override
    public void convertAndAdd(Process process) {
        FlowableListener createListener = new FlowableListener();
        createListener.setImplementation(AssigneeListener.class.getCanonicalName());
        createListener.setImplementationType("class");
        createListener.setEvent("create");

        UserTask userTask = new UserTask();
        userTask.setId(this.getCName());
        userTask.setName(this.getData().getLabel());
        // 任务创建监听器
        List<FlowableListener> taskListeners = new ArrayList<>();
        taskListeners.add(createListener);
        userTask.setTaskListeners(taskListeners);
        // 执行人处理
        String assignMultiStrategy = this.getData().getAssign().getAssignMultiStrategy();
        MultiInstanceLoopCharacteristics loopCharacteristics = new MultiInstanceLoopCharacteristics();
        if (StringUtils.equalsIgnoreCase(assignMultiStrategy, AssignMultiStrategy.SIGN_ORDER)) {
            loopCharacteristics.setSequential(true);
        }
        if (StringUtils.equalsIgnoreCase(assignMultiStrategy, AssignMultiStrategy.SIGN_ALL)
                || StringUtils.equalsIgnoreCase(assignMultiStrategy, AssignMultiStrategy.SIGN_ANY)) {
            loopCharacteristics.setSequential(false);
        }
        loopCharacteristics.setInputDataItem("${multiInstanceHandler.resolveAssignee(execution)}");
        loopCharacteristics.setElementVariable(userTask.getId() + "_assignee");
        loopCharacteristics.setCompletionCondition("${multiInstanceHandler.completionCondition(execution)}");
        userTask.setLoopCharacteristics(loopCharacteristics);
        String format = "${" + userTask.getId() + "_assignee}";
        userTask.setAssignee(format);
        process.addFlowElement(userTask);
    }

    @Override
    public Node parse(HashMap<String, Object> cell) {
        return JSON.parseObject(JSON.toJSONString(cell), NodeUserTask.class);
    }
}
